package com.tianpengtech.admin.account;
import com.jfinal.aop.Before;
import com.jfinal.kit.HashKit;
import com.jfinal.plugin.activerecord.Model;
import com.tianpengtech.admin.AbstractAdminController;
import com.tianpengtech.admin.IAdminInfoEdit;
import com.tianpengtech.admin.InitInterceptor;
import com.tianpengtech.admin.role.RoleDao;
import com.tianpengtech.common.GlobalConfigConstant;
import com.tianpengtech.common.exception.ServiceException;
import com.tianpengtech.common.exception.ValidateException;
import com.tianpengtech.common.model.account.Account;
import com.tianpengtech.common.model.authenticate.Authenticate;
import com.tianpengtech.common.util.JsonUtil;
import com.tianpengtech.common.util.XxxUtil;
import org.apache.log4j.Logger;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresRoles;

/**
* 路由配置地址：add("/admin/account", AccountAdminController.class, "/admin/account");
* 数据库映射地址：arp.addMapping(Account.dao.tableName(),Account.class);
*/
@Before(InitInterceptor.class)
@RequiresAuthentication
public class AccountAdminController extends AbstractAdminController{
	private Logger logger = Logger.getLogger(AccountAdminController.class);
	private IAdminInfoEdit editService =new AccountAdminServiceImpl();
	@Override
	protected void config() {
		this.clazz=Account.class;
		this.service=new AccountAdminServiceImpl();
	}

	@Override
	public void beforeAdd() {
		setAttr("roleList", RoleDao.ME.readAllRoles());
	}

	@Override
	public void beforeAdd(Model< ? > model) throws ValidateException {

		String userPass = GlobalConfigConstant.DEFAULT_PASS_WORD;
		userPass = XxxUtil.passMd5(userPass);
		logger.debug(userPass);
		model.set(Account.USERPASS,userPass);
	}

	@Override
	public void beforeQueryById(Model<?> model, int id) {
		setAttr("roleList", RoleDao.ME.readAllRoles());
	}


	@Override
	public void beforeUpdate(Model< ? > model) throws ValidateException {

	}

	@Override
	public void beforeDelete(Model< ? > model) {

	}
	
	
	/**
	 * 去向修改密码
	 */
	public void resetPass(){
		int id = getParaToInt(Account.ID,0);
		Account account = XxxUtil.readSessionInfo();
		setAttr("account", this.service.queryById(account,id));
		renderTemplate("reset-password-form.html");
	}
	
	
	
	/**
	 * 去向修改密码
	 */
	public void doResetPass(){
		try {
			String oldPass = getPara("oldpass",GlobalConfigConstant.DEFAULT_VALUE_EMPTY);
			String confirmPass = getPara("confirmpass",GlobalConfigConstant.DEFAULT_VALUE_EMPTY);
			String newPass = getPara("newpass",GlobalConfigConstant.DEFAULT_VALUE_EMPTY);
			Account account = XxxUtil.readSessionInfo();
			editService.resetPass(account, oldPass, newPass, confirmPass, null);
			renderJson(JsonUtil.printSuccess("密码修改成功，请在下次登录的时候使用新密码登录"));
		} catch (ValidateException e) {
			renderJson(JsonUtil.printError(e.getMessage()));
		} 
	}

	@Override
	public void parsePageRecord(Object results) {
		Account account = (Account)results;
		account.set(Account.STATUS,account.status());
	}

}

	